/**
政企专区首页
**/
//var isOrgInDeYang = false;
var zdxsUrl = "//dzgtest.vpclub.cn/dy/website/index.html";
/*
自定义模板规则：列表
*/
Handlebars.registerHelper('datas', function(items, options) {
  var itemArr = new Array();
  if(items instanceof Array){
    itemArr = items;
  }else if(items instanceof Object){
    itemArr.push(items);
  } 
  
  var out = '';
  var len = itemArr.length;
  for(var i=0; i<len; i++) {
    //var item = options.fn(items[i]);
    var item = itemArr[i];
    var code = item.code;
    var codeFlag = code.split("_")[1];
    var li = '<li class="am-text-center" id="'+code+'" onclick="pageView(\''+codeFlag+'\');">'
            + '<div class="am-margin-top-xs am-padding-lg border-radius item-background">'
                + '<div class="am-padding-horizontal-xs">'
                    + '<img class="am-circle" src="'+getPicUrl(code)+'"/>'
                + '</div>'
                // + '<div class="am-text-sm am-margin-top-lg am-text-truncate">'+item.name+'</div>'
                + '<div class="am-text-sm am-margin-top-lg name-box-larger">'+item.name+'</div>' // 让名字换行
            + '</div>'
        + '</li>';
    if("ZQ_EMOP" == code || 'ZQ_XHRW' === code){//政企营销只在android中显示
        if(isAndroid()){
            out += li;
        }
    }else if("ZQ_ZDXS" == code){//终端销售只有德阳的人可见
        //if(isOrgInDeYang){
            out += li;
            zdxsUrl = item.value;
        //}
    }else{
        out += li;
    }
  }

  return out;
});

var getPicUrl = function(code){
    var picUrl = "";
    if ("ZQ_KDYY" == code) {
        picUrl = "../../template/website/images/gov_broadband.png";
    } else if ("ZQ_ESOP" == code) {
        picUrl = "../../template/website/images/esop.png";
    } else if ("ZQ_JCFW" == code) {
        picUrl = "../../template/website/images/jcfw.png";
    } else if ("ZQ_EMOP" == code) {
        picUrl = "../../template/website/images/emop.png";
    } else if ("ZQ_ZDXS" == code) {
        picUrl = "../../template/website/images/zdxs.png";
    } else if ('ZQ_SMDJ' == code) {
        picUrl = "../../template/website/images/smdj.png";
    } else if ('ZQ_XHRW' == code) {
        picUrl = "../../template/website/images/xhrw.png";
    }

    return picUrl;
}

var setIndexUrlToCookie = function(){
    var myUrl = window.location.href;
    var c_govindexurl = Constants.COOKIEGOVINDEXURLPREFIX;
    setCookie(c_govindexurl,myUrl,0,"/");
    console.info("govIndexUrl = "+getCookie(c_govindexurl));
};

var getMyParams = function(){
    var urlParams = getParams();
    if(urlParams){
        urlParams = "?" + urlParams;
    }else{
       urlParams = "";
    }

    return urlParams;
};

var kdyyAction = function(urlParams,canAppShare){
    var toUrl = "../broadband_list.html" + urlParams;
    if(canAppShare == 1){
        toUrl += urlParams ? "&bd_type=2":"?bd_type=2";//必须传bd_type=2
        toUrl += "&canIndexShare=1";//首页是否可分享
    }else{
        //toUrl += urlParams ? "&bd_type=2&canAppShare=1":"?bd_type=2&canAppShare=1";//必须传bd_type=2
        //1.6.3版本商务动力宽带预约走原生页面，不能分享了
        toUrl += urlParams ? "&bd_type=2":"?bd_type=2";//必须传bd_type=2
    }
    toUrl = toUrl.replace("canAppShare=1&","")
    console.log("toUrl = "+toUrl);
    window.location.href = toUrl;
};

var esopAction = function(urlParams){
    window.location.href = "esop/index.html" + urlParams;
    // if(getParamValue("EmpCode") == "aaaX27"){//DEMO
    //     window.location.href = "esop_demo/login.html" + urlParams;
    // }else{
    //     window.location.href = "esop/index.html" + urlParams;
    // }
};

var zdxsAction = function(urlParams,canAppShare){
//    var host = window.location.host;
//    if(host.indexOf("dzgtest.vpclub.cn") == -1){
//        host = "dzgtest.vpclub.cn";
//    }
//
//    var toUrl = "http://"+ host +"/dy/website/index.html" + urlParams;
	var toUrl = zdxsUrl + urlParams;
    if(canAppShare != 1){
        toUrl += urlParams ? "&canAppShare=1":"?canAppShare=1";//让下级页面可分享
    }
    if(!getParamValue("saleType")){
        toUrl += "&saleType=1";//让下级页面有分享标记
    }

    window.location.href = toUrl;
}

var toActionByCode = function(code){
    var urlParams = getMyParams();
    console.info("urlParams = " + urlParams);
    
    var canAppShare = getParamValue("canAppShare");

    if ("ZQ_KDYY" == code) {
        kdyyAction(urlParams,canAppShare);
    } else if ("ZQ_ESOP" == code) {
        esopAction(urlParams);
    } else if ("ZQ_JCFW" == code) {
        window.location.href = "dzg://jcfw";
    } else if ("ZQ_EMOP" == code) {
        window.location.href = "dzg://emop";
    } else if ("ZQ_ZDXS" == code) {
        zdxsAction(urlParams,canAppShare);
    } else if ('ZQ_SMDJ' == code) {
        window.location.href = 'dzg://smdj';
    } else if ('ZQ_XHRW' == code) {
        window.location.href = 'dzg://qhdxhrw';
    }
};

var bindEvent = function(){
    $(".am-gallery > li").on("click",function(){
        var code = $(this).attr("id");
        toActionByCode(code);
    });
};

var showConfigInfo = function(data){
	//isOrgInDeYang = data.isOrgInDeYang;
    var $tpl = $('#datas-tpl');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
        data: data
    };

    var html = template(myData);
    $(".am-gallery").empty();
    $(html).appendTo($(".am-gallery"));
    bindEvent();
};

var loadConfig = function(){
    var params = {"serviceType":"ZQ_INDEX_H5","userId":getParamValue("userId"),"groupId":getParamValue("group_id")};
    var reqUrl = Constants.apiV2Host + "h5/config/getProfileListByUserInfo";

    sendRestPostAjax(params,reqUrl,showConfigInfo);
};

$(function () {
    //首页url地址保存到cookie，下级页面返回时用
    setIndexUrlToCookie();

    loadConfig();
});